using UnityEngine;
using QFramework;
using System.Collections;
using UnityEditor;


// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
	public partial class Enemy_Slime : EnemyFathers
	{

        [Header("是否已经死亡")]
        public bool isDie = false;


        public override BoxCollider2D OnOwer => HitHurtBox;
        //protected override Collider2D Collider2D => HitHurtBox;
        /// <summary>
        /// 敌人生命值
        /// </summary>
        public float enemyHp = 30;

        /// <summary>
        /// 敌人移动速度
        /// </summary>
        public float enemySpeed = 2;

        private bool mIgnoreHurt = false;

        public override float EnemyHp { get => enemyHp; set { if (value < 0) value = 0; enemyHp = value; } }
        public override float EnemyMoveSpeed { get => enemySpeed; set=>enemySpeed=value; }


        

        /// <summary>
        /// 默认朝向右边
        /// </summary>
        //private bool isFacingRight = true;
        public override void Awake()
        {
            base.Awake();
            HitHurtBox.OnTriggerEnter2DEvent(collider =>
            {
                var hurt = collider.GetComponent<HitHurtBox>();
                if (hurt!=null)
                {
                    if (hurt.Owner.CompareTag("Player"))
                    {
                        //触发伤害计算
                        var player = hurt.Owner.GetComponent<Player>();
                        if (player != null) 
                        {
                            DataManager.Instance.playerModel.PlayerHp.Value -= 1;
                        }
                    }
                }
            });
        }


      

        public override void Start()
        {
            base.Start();
        }


        //private void Update()
        //{
        //    if (isDie)
        //    {

        //    }
        //    else
        //    {
        //        MoToPlayer();
        //    }

        //   // Die();
        //}

        public override void Update()
        {
            base.Update();
            if (isDie)
            {
                Die();
            }
            else
            {
                MoToPlayer();
            }
        }


        public override void Hurt(float value, bool force = false, bool critical = false)
        {
            base.Hurt(value, force, critical);

            if (mIgnoreHurt && !force)
                return;
            mIgnoreHurt = true;
            transform.Translate(Vector2.zero);
            FloatingTextController.Play(this.transform.position + Vector3.up * 0.5f, value.ToString("0"), critical);
            this.SlimeRender.color = Color.red;
            ActionKit.Delay(0.2f, () =>
            {
                //AudioKit.PlaySound("Hit"); 
                this.enemyHp -= value;
                this.SlimeRender.color = Color.white;
                mIgnoreHurt = false;
            }).Start(this);

            if (EnemyHp <= 0)
            {

                isDie = true;
                //Die();
               
            }
        }

       

        public override void MoToPlayer()
        {
            base.MoToPlayer();
            if (!mIgnoreHurt)
            {
                if (Player.Instance)
                {
                    var dic = (Player.Instance.transform.position - transform.position).normalized;

                    FaceToTarget(Player.Instance.transform.position);

                    transform.Translate(dic * EnemyMoveSpeed * Time.deltaTime);
                }
                else
                {
                    transform.Translate(Vector2.zero);
                }
            }
        }
        public override void Die()
        {
            base.Die();
            //enemySpeed =0;
            DataManager.Instance.abilityUpModel.SkillEnemyNumbers.Value += 1;
            DataManager.Instance.gameDataModel.KillEnemysIndex.Value += 1;
            DataManager.Instance.survivalPowerModel.AbilityKillNumber.Value += 1;
            StartCoroutine(DieAnimPlaye());
        }


        IEnumerator DieAnimPlaye()
        {
        
            transform.Translate(Vector2.zero);
            HitHurtBox.GetComponent<BoxCollider2D>().enabled = false;
            SelfAnimator.Play("EnemyGhostAnim_Die");
            yield return null;

            var playerTime = SelfAnimator.GetCurrentAnimatorStateInfo(0);
            float waitTime = playerTime.length;
            yield return new WaitForSeconds(waitTime);
            GameManager.Instance.InsExpobj(this.transform);
            PoolManager.Instance.Push(this.gameObject);
            //this.DestroyGameObjGracefully();  
        }

      
        public override void SetLeftScale()
        {
            base.SetLeftScale();
           
        }
        public override void SetRightScale()
        {
            base.SetRightScale();
           
        }

        /// <summary>
        /// 每次启用时，重新设置自身的属性
        /// </summary>
        private void OnEnable()
        {
            RemoveStart();
        }

        private void OnDisable()
        {
            SetDie();
        }

        public override void SetDie()
        {
            base.SetDie();

            //EnemyInsManager.Instance.SetEnemyData(this.gameObject);
            // EnemyHp = 
            HitHurtBox.GetComponent<BoxCollider2D>().enabled = false;
            SelfAnimator.Play("EnemyGhostAnim");
            //MoToPlayer();
        }

        public override void RemoveStart()
        {
            base.RemoveStart();
            isDie = false;
            HitHurtBox.GetComponent<BoxCollider2D>().enabled = true;
            SelfAnimator.Play("EnemyGhostAnim");
        }

    }
}
